package com.situ.mall.controller;

import com.situ.mall.pojo.User;
import com.situ.mall.pojo.query.UserQuery;
import com.situ.mall.service.ILoginLogService;
import com.situ.mall.service.IUserService;
import com.situ.mall.util.IPUtil;
import com.situ.mall.util.LayUITableResult;
import com.situ.mall.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private IUserService userService;
    @Autowired
    private ILoginLogService loginLogService;

    @RequestMapping("/login")
    @ResponseBody
    public Result login(HttpServletRequest req, String username, String password, String code, HttpSession session) {
        String codeInSession = (String) session.getAttribute("codeInSession");
        if (!codeInSession.equalsIgnoreCase(code)) {
            return Result.error("验证码错误");
        }
        User user = userService.login(username, password);
        if (user != null) {
            boolean isSuccess = userService.selectStatus(user);
            if (!isSuccess) {
                loginLogService.recordLoginLog(user.getUsername(), 1, IPUtil.getIPAddress(req), "登录失败");
                return Result.error("用户已被禁用");
            } else {
                System.out.println(user.toString());
                session.setAttribute("user", user);
                loginLogService.recordLoginLog(user.getUsername(), 1, IPUtil.getIPAddress(req), "登录成功");
                return Result.ok("登录成功");
            }
        } else {
            //loginLogService.recordLoginLog(user.getUsername(), 1, IPUtil.getIPAddress(req), "登录失败");
            return Result.error("用户名或密码错误");
        }
    }

    @RequestMapping("/getLoginPage")
    public String getLoginPage() {
        return "login";
    }

    @RequestMapping("/getUserListPage")
    public String getUserListPage() {
        return "user_list";
    }

    @RequestMapping("/selectByPage")
    @ResponseBody
    public LayUITableResult selectByPage(UserQuery userQuery) {
        return userService.selectByPage(userQuery);
    }

    @RequestMapping("/deleteById")
    @ResponseBody
    public Result deleteById(Integer id) {
        boolean isSuccess = userService.deleteById(id);
        return isSuccess ? Result.ok("删除成功") : Result.error("删除失败");
    }

    @RequestMapping("/deleteAll")
    @ResponseBody
    public Result deleteAll(Integer[] ids) {
        boolean isSuccess = userService.deleteAll(ids);
        return isSuccess ? Result.ok("删除成功") : Result.error("删除失败");
    }

    @RequestMapping("/updateStatus")
    @ResponseBody
    public Result updateStatus(Integer id, Byte status) {
        boolean isSuccess = userService.updateStatus(id, status);
        return isSuccess ? Result.ok("更新成功") : Result.error("更新失败");
    }

    @RequestMapping("/getUserInsertPage")
    public String getUserInsertPage() {
        return "user_insert";
    }

    @RequestMapping("/insert")
    @ResponseBody
    public Result insert(User user) {
        boolean isSuccess = userService.insert(user);
        return isSuccess ? Result.ok("添加成功") : Result.error("添加失败");
    }

    @RequestMapping("/getUserUpdatePage")
    public String getUserUpdatePage(Integer id, Model model) {
        User user = userService.selectUpdateUser(id);
        model.addAttribute("user", user);
        return "user_update";
    }

    @RequestMapping("/update")
    @ResponseBody
    public Result update(User user) {
        boolean isSuccess = userService.update(user);
        return isSuccess ? Result.ok("更新成功") : Result.error("更新失败");
    }

    @RequestMapping("/logout")
    public String logout(HttpSession session) {
        session.invalidate();
        return "/";
    }
}
